<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

/*if(isset($_REQUEST['grup'])){	
	switch($_REQUEST['grup']){
		case 20://verksamhet
			$g=10;//Ledningsgrup
		break;
		case 50://studiekonsulent
			$g=20;//verksamhet
		break;		
		case 70://cirkelLedare
			$g=50;//studiekonsulent
		break;
		case 90://medlem
			$g=70;//cirkelledare
		break;
	}
	//$owners = $MAIN->users->getGroupOwners($g);
	$tmpUser=$MAIN->users->getUser($_REQUEST['uid']);
	?>
	<select name="ddl_owner" id="ddl_owner" class="validate-custom-required emptyValue:'--'">
		<option value="--">-----</option>
	<?php
	foreach($owners as $owner){
		if($owner['firstname']!=""){
			$owner['display_name']=$owner['firstname']." ".$owner['lastname'];
		}else{
			$owner['display_name']=$owner['email'];
		}
	?>
		<option <?php if($tmpUser['owner_id']==$owner['id']){?>selected="selected"<?php } ?> value="<?php echo($owner['id']);?>"><?php echo($owner['display_name'])?></option>
	<?php
	}
	?>	
	</select>
	<?php
	exit();
}*/

if(isset($_REQUEST['getUser'])){
	$user=$MAIN->users->getUser($_REQUEST['id']);
	?>
	<h2>Användare</h2>
	<form name="frm_user" id="frm_user" action="persons.php" method="post" onsubmit="return false;">
	<input type="hidden" name="txt_user_id" value="<?php echo($user['id']);?>"/>
	<input type="hidden" name="txt_person_id" value="<?php echo($_REQUEST['pid']);?>"/>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>E-post address</td>
			<td class="right"><input class="required validate-email" autocomplete="off" type="text" <?php if($_REQUEST['id']!=0){?>readonly="readonly"<?php }?> name="txt_email" value="<?php echo(@$user['email']);?>"/></td>			
		</tr>		
		<tr>
			<td>Lösenord</td>
			<td class="right"><input class="required" autocomplete="off" type="password" <?php if($_REQUEST['id']!=0){?>readonly="readonly"<?php }?> name="txt_pass" value="<?php if($user['drowp']!=""){echo($MAIN->decode(@$user['drowp']));}?>"/></td>
		</tr>
		<tr>
			<td>Grupp</td>
			<td class="right">
				<?php
				$groups = $MAIN->groups->getGroups();
				?>
				<select name="ddl_rights" class="validate-custom-required emptyValue:'--'" <?php if($_REQUEST['id']!=0){?>disabled="disabled"<?php }?>>
					<option value="--">-- ange gruppnivå --</option>
				<?php
				foreach($groups as $g){
					if($MAIN->currentUser['grouplevel']<$g['rights'] || $MAIN->currentUser['grouplevel']==0){
				?>
					<option value="<?php echo($g['rights']);?>" <?php if($user['grouplevel']==$g['rights']){?>selected="selected"<?php }?>><?php echo($g['display_name']);?></option>
				<?php
					}
				}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="right">
				<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_user');"/>
			</td>
		</tr>
	</table>
	</form>
	<?php
	exit();
}

if(isset($_POST['txt_email'])){
	if($userId = $MAIN->users->setUser($_POST['txt_user_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=users&id=<?php echo($userId);?>&view=edit';}).delay(3000);</script>
	<?php
	}
	exit();
}
if(isset($_POST['txt_person_firstname'])){
	if($MAIN->persons->setPerson($_POST['txt_person_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');</script>
	<?php
	}
	exit();
}
if(isset($_REQUEST['del'])){
	if($MAIN->users->delUser($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=users&view=all';}).delay(3000);</script>
	<?php	
	};
}
if(isset($_REQUEST['act'])){
	$MAIN->users->toggleActivate($_REQUEST['act']);
	?>
	<script type="text/javascript">
		document.location.href='index.php?cat=users';
	</script>
	<?php
	exit();
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
	$ord="ASC";
	$orderBy="lastname";
	if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
	$nextOrd="DESC";
	if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
	$users = $MAIN->users->getUsers($orderBy,$ord);	
?>
	<h1>Alla registrerade användare</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>		
		<th><a href="?cat=users&amp;view=all&amp;ordby=firstname&amp;ord=<?php echo($nextOrd);?>">Förnamn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=users&amp;view=all&amp;ordby=lastname&amp;ord=<?php echo($nextOrd);?>">Efternamn</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=users&amp;view=all&amp;ordby=grouplevel&amp;ord=<?php echo($nextOrd);?>">Grupp</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=users&amp;view=all&amp;ordby=email&amp;ord=<?php echo($nextOrd);?>">E-postadress</a></th>
		<th style="width:10px;"></th>		
		<!--<th><a href="?cat=users&amp;view=all&amp;ordby=pers&amp;ord=<?php echo($nextOrd);?>">Person nr.</a></th>
		<th style="width:10px;"></th>-->
		<th><a href="?cat=users&amp;view=all&amp;ordby=tel&amp;ord=<?php echo($nextOrd);?>">Tel</a></th>
		<th style="width:10px;"></th>
		<th>Administrera</th>		
		<th style="width:10px;"></th>
		<th><a href="?cat=users&amp;view=all&amp;ordby=last_login&amp;ord=<?php echo($nextOrd);?>">Sista inloggning</a></th>
	</tr>
	<?php
	foreach($users as $user){
	?>
	<tr class="entry">		
		<td><?php echo($user['firstname']);?></td>
		<td></td>
		<td><?php echo($user['lastname']);?></td>
		<td></td>
		<td><?php echo($MAIN->groups->getGroupName($user['grouplevel']));?></td>
		<td></td>
		<td><?php echo($user['email']);?></td>
		<td></td>
		<!--<td><?php echo($user['pers']);?></td>
		<td></td>-->
		<td><?php echo($user['tel']);?></td>
		<td></td>
		<td class="tools">
			<?php
			if($MAIN->currentUser['id']!=$user['id']){
				if($MAIN->currentUser['grouplevel']<$user['grouplevel'] || $MAIN->currentUser['grouplevel']==0){
				?>
				<a href="javascript:void(0);" onclick="delUser(<?php echo($user['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>
				<?php if($user['active']==0){?>
				<a href="javascript:void(0);" onclick="toggleActivate(<?php echo($user['id']);?>);"><img src="images/btn_not_active.png" alt="Aktivera" title="Aktivera"/></a>
				<?php }else{?>
				<a href="javascript:void(0);" onclick="toggleActivate(<?php echo($user['id']);?>);"><img src="images/btn_active.png" alt="Inaktivera" title="Inaktivera"/></a>
				<?php }?>
			
			<?php
				}
			}
			?>			
			<?php
			if($MAIN->currentUser['id']==$user['id']){
			?>	
			<a href="?cat=profile"><img src="images/btn_admin_edit.png" alt="Redigera" title="Redigera"/></a>
			<?php
			}else{
			?>
			<a href="?cat=users&amp;view=edit&amp;id=<?php echo($user['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera" title="Regigera"/></a>
			<?php
			}
			?>
		</td>
		<td></td>
		<td>&nbsp;&nbsp;<?php if($user['last_login']!=""){echo(date('Y-m-d H:s',strtotime($user['last_login'])));}?></td>
	</tr>
	<?php
	}
	?>
	</table>
	<script type="text/javascript">
		var delUser = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('users.php','del='+id);
			}
		}
		var toggleActivate = function(uid){
			var msg='Bekräfta aktivering/inaktivering!';
			if(confirm(msg)){
				ajaxGet('users.php','act='+uid);
			}
		}
	</script>
<?php
	break;
	case 'add':
	case 'edit':
		$user = $MAIN->users->getUser($_REQUEST['id']);	
?>
	<h1>Redigera användare</h1>
	<table>
		<tr>
			<td>
	<form name="frm_user" id="frm_user" action="users.php" method="post" onsubmit="return false;">
	<input type="hidden" name="txt_user_id" value="<?php echo($user['id']);?>"/>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>E-post address</td>
			<td class="right"><input class="required validate-email" autocomplete="off" type="text" <?php if($_REQUEST['view']!='add'){?>readonly="readonly"<?php }?> name="txt_email" value="<?php echo(@$user['email']);?>"/></td>
		</tr>		
		<tr>
			<td>Lösenord</td>
			<td class="right"><input class="required" autocomplete="off" type="password" name="txt_pass" value="<?php if($user['drowp']!=""){echo($MAIN->decode(@$user['drowp']));}?>"/></td>
		</tr>		
		<tr>
			<td>Grupp</td>
			<td class="right">
				<?php
				$groups = $MAIN->groups->getGroups();
				?>
				<select name="ddl_rights" id="ddl_rights" class="validate-custom-required emptyValue:'--'">
					<option value="--">-- ange gruppnivå --</option>
				<?php
				foreach($groups as $g){
					if($MAIN->currentUser['grouplevel']<$g['rights'] || $MAIN->currentUser['grouplevel']==0){
				?>
					<option value="<?php echo($g['rights']);?>" <?php if($user['grouplevel']==$g['rights']){?>selected="selected"<?php }?>><?php echo($g['display_name']);?></option>
				<?php
					}
				}
				?>
				</select>
			</td>
		</tr>
		<!--<tr>
			<td>Ansvarig</td>
			<td class="right">
				<div id="cnt_owner">
					<select name="ddl_owner" id="ddl_owner" class="validate-custom-required emptyValue:'--'">
						<option value="--">-----</option>
					</select>
				</div>
			</td>
		</tr>-->
		<tr>
			<td>Förening</td>
			<td class="right">
				<?php
				$associations = $MAIN->associations->getAssociations();				
				?>
				<select id="ddl_association" name="ddl_association" class="validate-custom-required emptyValue:'--'">
					<option value="--">-- ange förening --</option>
					<?php
					foreach($associations as $as){
					?>
					<option <?php if($user['association_id']==$as['id']){?>selected="selected"<?php }?> value="<?php echo($as['id']);?>"><?php echo($as['name']);?></option>
					<?php
					}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="right">
				<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_user');"/>
			</td>
		</tr>
	</table>
	</form>	
			</td>
			<td>				
				<div class="notes">
					<?php 
					if(isset($_REQUEST['id'])&&$_REQUEST['id']!=0){
					?>
					<h2 style="text-align:right;">Allmänna notiser</h2>
					<div class="note" style="width:410px; text-align:right;">
						<textarea name="txt_note" id="txt_note" rows="" cols=""></textarea>
						<div class="tools" style="padding:0px;">
							<input type="submit" name="btn_submit" value="Spara" onclick="ajaxGet('note.php','rel_table=users&id=<?php echo($user['id']);?>&text='+$('txt_note').value);$('txt_note').value='';"/>
						</div><br/>
					</div>				
					<div class="cnt_notes" id="cnt_notes">
					</div>
					<script type="text/javascript">
					ajaxGet('notes.php','rel_table=users&rel_id=<?php echo($user['id']);?>',$('cnt_notes'));
					</script>
					<?php }?>
						
				</div>
			</td>
		</tr>
	</table>
	<script type="text/javascript">
		/*var getOwners = function(){
			if($('ddl_rights').value<=10 || $('ddl_rights').value==90){
				$('ddl_owner').set("disabled","disabled");
				$('ddl_owner').value=0;
			}else{
				$('ddl_owner').set("disabled","");
				ajaxGet('users.php','grup='+$('ddl_rights').value+'&uid=<?php echo($user['id']);?>',$('cnt_owner'));
			}			
		}
		$('ddl_rights').addEvent('change', function(){
			getOwners();
		});
		getOwners();*/
		$('ddl_rights').addEvent('change', function(){//validate-custom-required emptyValue:'--'
			ïf(this.value<=20){
				$('ddl_association').set('disabled','disabled');
				$('ddl_association').removeClass('validate-custom-required');				
			}else{
				$('ddl_association').set('disabled','');
				$('ddl_association').addClass('validate-custom-required');
			}
		});
	</script>
	<hr/>
	<?php
	if($_REQUEST['view']!='add'){
	?>	
	<h2>Användaruppgifter kopplade till profil</h2>
	<form name="frm_profile" id="frm_profile" action="users.php" method="post" onsubmit="return false;">
		<?php
		if($user['person_id']==''){$user['person_id']=0;}
		?>
		<input type="hidden" name="txt_person_id" value="<?php echo($user['person_id']);?>"/>
		<input type="hidden" name="txt_user_id" value="<?php echo($user['id']);?>"/>
		<table cellpadding="0" cellspacing="0">
			<tr>
				<td>Förnamn</td>
				<td class="right"><input type="text" name="txt_person_firstname" value="<?php echo(@$user['firstname']);?>"/></td>
			</tr>
			<tr>
				<td>Efternamn</td>
				<td class="right"><input type="text" name="txt_person_lastname" value="<?php echo(@$user['lastname']);?>"/></td>
			</tr>
			<tr>
				<td>Personnummer</td>
				<td class="right"><input type="text" class="required validate-pers" name="txt_person_pers" value="<?php echo(@$user['pers']);?>"/></td>
			</tr>
			<tr>
				<td>Adress</td>
				<td class="right"><input type="text" name="txt_person_address" value="<?php echo(@$user['address']);?>"/></td>
			</tr>
			<tr>
				<td>Postnummer</td>
				<td class="right"><input type="text" name="txt_person_zip" value="<?php echo(@$user['zip']);?>"/></td>
			</tr>
			<tr>
				<td>Ort</td>
				<td class="right"><input type="text" name="txt_person_city" value="<?php echo(@$user['city']);?>"/></td>
			</tr>
			<tr>
				<td>Telefonnummer</td>
				<td class="right"><input type="text" name="txt_person_tel" value="<?php echo(@$user['tel']);?>"/></td>
			</tr>
			<tr>
				<td colspan="2" class="right">
					<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_profile');"/>
				</td>
			</tr>
		</table>
	</form>
	<hr/>
	<?php
	}
	?>
	
	<?php
	if($_REQUEST['view']!='add'){
	?>
	<h2>Loggar</h2>
	<?php
	$_REQUEST['user_id']=$user['id'];
	$_REQUEST['log_type']="user";
	include_once("logs.php");
	?>
	<?php
	}
	?>
<?php
	break;
}
?>
</div>